package com.libre.auth.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.libre.auth.entity.Permission;
import com.libre.auth.service.PermissionService;
import com.libre.common.base.BaseController;
import com.libre.common.enums.ResultEnum;
import com.libre.common.utils.PageUtil;
import com.libre.common.utils.ResultUtil;
import com.libre.common.vo.PageVo;
import com.libre.common.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @description   菜单资源
* @author        庞留杰
* @date          2021/12/19 18:59
* @version       V1.0
**/
@Slf4j
@RequestMapping("/permission")
@RestController
public class PermissionController  extends BaseController {

    @Autowired
    private PermissionService service;

    //查询带分页列表
    @GetMapping("/findPage")
    public Result<IPage<Permission>> findPage(@ModelAttribute PageVo pageVo, @ModelAttribute Permission permission) {
        IPage<Permission> page = service.findPage(PageUtil.initMpPage(pageVo),permission);
        return new ResultUtil<IPage<Permission>>().setData(page);
    }

    //查询不带分页列表
    @GetMapping("/findList")
    public Result<List<Permission>> findList(@ModelAttribute Permission permission) {
        List<Permission> list = service.findList(permission);
        return new ResultUtil<List<Permission>>().setData(list);
    }

    //通过ID查询
    @GetMapping("/findById")
    public Result<Permission> findById(String id) {
        Permission permission = service.findById(id);
        return new ResultUtil<Permission>().setData(permission);
    }

    //添加
    @PostMapping("/save")
    public Result save(@RequestBody Permission permission) {
        boolean boo = service.add(permission);
        if(boo){
            return new ResultUtil().setSuccessMsg(ResultEnum.OK.getReasonPhraseCN());
        }else {
            return new ResultUtil().setErrorMsg(ResultEnum.FAIL.getReasonPhraseCN());
        }
    }

    //修改
    @PostMapping("/update")
    public Result update(@RequestBody Permission permission) {
        boolean boo = service.update(permission);
        if(boo){
            return new ResultUtil().setSuccessMsg(ResultEnum.OK.getReasonPhraseCN());
        }else {
            return new ResultUtil().setErrorMsg(ResultEnum.FAIL.getReasonPhraseCN());
        }
    }

    //删除
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable String id) {
        boolean boo = service.delete(id);
        if(boo){
            return new ResultUtil().setSuccessMsg(ResultEnum.OK.getReasonPhraseCN());
        }else {
            return new ResultUtil().setErrorMsg(ResultEnum.FAIL.getReasonPhraseCN());
        }
    }

    // 权限树
    @GetMapping("/permissionTree")
    public Result<List<Permission>> permissionTree(@ModelAttribute Permission permission) {
        List<Permission> list = service.menuTree(permission);
        return new ResultUtil<List<Permission>>().setData(list);
    }

    // 权限树
    @GetMapping("/menuTree")
    public Result<List<Permission>> menuTree(@ModelAttribute Permission permission) {
        List<Permission> list = service.menuTree(permission);
        return new ResultUtil<List<Permission>>().setData(list);
    }

}
